import platform
from enum import Enum

from PySide6.QtCore import QLocale
from qfluentwidgets import (
    ConfigSerializer,
    QConfig,
    ConfigItem,
    BoolValidator,
    OptionsConfigItem,
    OptionsValidator,
    Theme,
    qconfig,
    ConfigValidator,
)


class Language(Enum):
    CHINESE_SIMPLIFIED = QLocale(QLocale.Language.Chinese, QLocale.Country.China)
    ENGLISH = QLocale(QLocale.Language.English, QLocale.Country.UnitedStates)
    AUTO = QLocale()


class LanguageSerializer(ConfigSerializer):
    """Language serializer"""

    def serialize(self, language):
        return language.value.name() if language != Language.AUTO else "Auto"

    def deserialize(self, value: str):
        return Language(QLocale(value)) if value != "Auto" else Language.AUTO


def is_win11():
    return platform.system() == "Windows" and "10.0.22" in platform.version()


class Config(QConfig):
    micaEnabled = ConfigItem("MainWindow", "MicaEnabled", is_win11(), BoolValidator())
    language = OptionsConfigItem(
        "MainWindow",
        "Language",
        Language.AUTO,
        OptionsValidator(Language),
        LanguageSerializer(),
        restart=True,
    )
    dpiScale = OptionsConfigItem(
        "MainWindow",
        "DPIScale",
        "Auto",
        OptionsValidator([1, 1.25, 1.5, 1.75, 2, "Auto"]),
        restart=True,
    )

    # 主窗口大小
    main_highlighter = OptionsConfigItem(
        "MainWindow", "MainHighlighter", 960, ConfigValidator()
    )
    main_wide = OptionsConfigItem("MainWindow", "MainWide", 780, ConfigValidator())


ZH_SUPPORT_URL = "https://qfluentwidgets.com/zh/price/"
EN_SUPPORT_URL = "https://qfluentwidgets.com/price/"

cfg = Config()
cfg.themeMode.value = Theme.AUTO
cfg.high = 960
cfg.wide = 780
qconfig.load("resources/config/config.json", cfg)

if __name__ == "__main__":
    print(is_win11())
    for i in Language:
        print(i.value.name())
